﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Volume Control
* Version: 0.2
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["volume"] = new Object();
myPlugins["volume"]["name"] = getLocalizedString2("mod_sysm_plugin_vol_name");
myPlugins["volume"]["onlyonce"] = true;
myPlugins["volume"]["newObject"] = function (NewSettings) { return new plugin_volume_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_volume_sensor(NewSettings) {
	// Required variables
	this.plugin = "volume";
	this.settings = NewSettings;
	this.edit = plugin_volume_edit;
	this.ReDraw = plugin_volume_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolor) this.settings.barcolor = "#7F7F7F";


	//The Sensor
	this.frame.onMultiClick = function ( ) { plugin_volume_mute.call(obj); };
	this.volume = new class_PercentBar(this.frame,0,0,getLocalizedString2("mod_sysm_plugin_vol_label"));
	this.volume.PercentColor(this.settings.color);
	this.volume.background.onMouseDrag = function ( ) { plugin_volume_change.call(obj); };
	this.volume.background.onMouseDown = function ( ) { plugin_volume_show.call(obj); };
	this.volume.background.onMouseUp = function ( ) { plugin_volume_hidde.call(obj); };

	this.slider = new Image();
	this.slider.visible = false;
	this.slider.src = "Resources/Buttons/Volume.png";
	this.slider.vOffset = 0;
	this.slider.width = 14 * mod_sysmonitor_size / 10;
	this.slider.height = 14 * mod_sysmonitor_size / 10;
	this.frame.appendChild(this.slider);


	// Update timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = 1;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_volume_update.call(obj); };
	plugin_volume_update.call(obj);


	// Calculating sensore height (required!)
	this.frame.height = this.volume.background.vOffset + this.volume.background.height;
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_volume_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_barcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor;

		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.volume.PercentColor(this.settings.color);
			this.settings.barcolor = Drive_Edit_DialogResults[1];
			this.volume.PercentBarColor(this.settings.barcolor);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	function plugin_volume_update() {
		if(system.mute) {
			this.volume.PercentBarValue(system.volume * 6.25,getLocalizedString2("mod_sysm_plugin_vol_mute"));
		} else {
			this.volume.PercentBarValue(system.volume * 6.25,getLocalizedString2("mod_sysm_plugin_vol_unmute"));
		}
		return true;
	}

	function plugin_volume_mute() {
		system.mute = !system.mute;
		plugin_volume_update.call(this);
		return true;
	}

	function plugin_volume_change() {
		var newvolume = ((system.event.x / (mod_sysmonitor_size / 10)) - 4) / 6.25;
		if(newvolume<0) newvolume=0;
		if(newvolume>16) newvolume=16;
		system.volume = newvolume;

		if((system.event.x / (mod_sysmonitor_size / 10)) > 2 && (system.event.x / (mod_sysmonitor_size / 10))< 103)
			this.slider.hOffset = this.volume.background.hOffset + system.event.x - (this.slider.width/2);

		plugin_volume_update.call(this);
		return true;
	}

	function plugin_volume_show() {
		this.slider.hOffset = this.volume.right.hOffset - (this.slider.width/2);
		this.slider.visible = true;


		var newvolume = ((system.event.x / (mod_sysmonitor_size / 10)) - 4) / 6.25;
		if(newvolume<0) newvolume=0;
		if(newvolume>16) newvolume=16;
		system.volume = newvolume;

		if((system.event.x / (mod_sysmonitor_size / 10)) > 2 && (system.event.x / (mod_sysmonitor_size / 10))< 103)
			this.slider.hOffset = this.volume.background.hOffset + system.event.x - (this.slider.width/2);

		plugin_volume_update.call(this);


		return true;
	}

	function plugin_volume_hidde() {
		this.slider.visible = false;
		return true;
	}

	function plugin_volume_ReDraw() {
		this.frame.width = mod_sysmonitor_width;
		this.volume.style = preferences.mod_sysmonitor_bar_style.value;
		this.volume.size = mod_sysmonitor_size;
		this.slider.width = this.slider.srcWidth * mod_sysmonitor_size / 10;
		this.slider.height = this.slider.srcHeight * mod_sysmonitor_size / 10;
		this.frame.height = this.volume.background.vOffset + this.volume.background.height;
		return true;
	}

}

// --------------------------------------------------------------------------------
// The END